//
//  MusicController.m
//  PacIsBack
//
//  Created by Simon André on 30/01/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "MusicController.h"


@implementation MusicController

static MusicController* singletonInstance;

@synthesize player;

+ (MusicController*)instance
{
	@synchronized(self)
    {
		if (singletonInstance == NULL)
			singletonInstance = [[self alloc] init];
    }
	return(singletonInstance);
}


-(void) playMusic:(NSString*) fileName{
	NSString *soundPath = [[NSBundle mainBundle] pathForResource:fileName ofType:@"mp3"];
	AVAudioPlayer* audioPlayer =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:soundPath] error:NULL];
	[self setPlayer:audioPlayer];
	[audioPlayer release];
	[[self player] prepareToPlay];
	//number of loop inferior to 0 for infinite loop
	[[self player] setNumberOfLoops:-2];
	[[self player] play];
}

-(void) stopMusic{
	if ([self player] != nil) {
		[[self player] stop];
		[self setPlayer:nil];
	}
}

- (void) dealloc{
	[self setPlayer:nil];
	[super dealloc];
}

@end
